import { SysAop } from '@ohos/sysaop';
import { startAop2 } from './AopCase2';

const LOG_TAG = "[SysAopTag] ";

export function hookEnableBluetooth() {
  SysAop.addBefore("@ohos.bluetooth.access", "enableBluetooth", true, () => {
    console.info(LOG_TAG + "access.enableBluetooth() monitored from sysaop before execute.");
  });

  SysAop.addAfter("@ohos.bluetooth.access", "enableBluetooth", true, () => {
    console.info(LOG_TAG + "access.enableBluetooth() monitored from sysaop after execute.");
  });
}

export function hookHttp() {
  SysAop.addBefore("@ohos.net.http", "createHttp", true, () => {
    console.info(LOG_TAG + "http.createHttp() monitored from sysaop before execute.");
  });

  // 有返回值的函数，使用addAfter时，需要在after函数中主动返回，after函数的参数为模块/类实体、返回值。
  SysAop.addAfter("@ohos.net.http", "createHttp", true, (instance, result) => {
    console.info(LOG_TAG + "http.createHttp() monitored from sysaop after execute.");
    if (result === undefined) {
      return result;
    }

    let resultNew = {};
    Object.getOwnPropertyNames(result).forEach(function (key) {
      resultNew[key] = result[key];
    });

    // monkey patch
    resultNew["request"] = function () {
      console.info(LOG_TAG + "httpRequest.request() request website: " + arguments[0]);
      return result["request"].apply(this, arguments);
    }

    return resultNew;
  });
}

export function hookUICtxPrompt() {
  SysAop.addBefore("@ohos.arkui.UIContext", "PromptAction.showToast", false, () => {
    console.info(LOG_TAG + "arkui.UIContext.PromptAction.showToast() monitored : " + (new Error()).stack);
  });
  SysAop.addBefore("@ohos.arkui.UIContext", "PromptAction.showToast", false, () => {
    console.info(LOG_TAG + "arkui.UIContext.PromptAction.showToast() monitored2222 : " + (new Error()).stack);
  });
}

export function hookHmsAuth() {
  SysAop.addBefore("@hms.core.authentication", "HuaweiIDProvider.createLoginWithHuaweiIDRequest", false, () => {
    console.info(LOG_TAG + "HuaweiIDProvider.createLoginWithHuaweiIDRequest() monitored : " + (new Error()).stack);
  });
}

export function hookConstructor() {

  SysAop.addBefore("@ohos.arkui.UIContext", "PromptAction.constructor", false, (...args) => {
    console.info(LOG_TAG + "arkui.UIContext.PromptAction.constructor() addBefore 1: " + args);
    // constructor的addBefore不能有返回值
  });

  SysAop.addBefore("@ohos.arkui.UIContext", "PromptAction.constructor", false, (...args) => {
    console.info(LOG_TAG + "arkui.UIContext.PromptAction.constructor() addBefore 2: " + args);
  });

  SysAop.addAfter("@ohos.arkui.UIContext", "PromptAction.constructor", false, (obj, args) => {
    console.info(LOG_TAG + "arkui.UIContext.PromptAction.constructor() addAfter 1: " + obj);
    // constructor的addAfter可以有返回值，替换了原构造函数构造出的对象
  });

  SysAop.addAfter("@ohos.arkui.UIContext", "PromptAction.constructor", false, (obj, args) => {
    console.info(LOG_TAG + "arkui.UIContext.PromptAction.constructor() addAfter 2: " + obj);
  });
}

export function startAop() {
  hookEnableBluetooth();
  hookHttp();
  startAop2();
  // hookUICtxPrompt();
  hookHmsAuth();
  hookConstructor()
}